<template>
  <!-- visible也可以设置一个sync修饰符 但是这里不能设置 -->
  <!-- el-dialog 中的visible.sync是给el-dialog组件设置的，也就是组件里会触发事件this.$emit("update:visible",false) -->
  <!-- 因为这里的visible后面的值是一个props属性 而props是父组件的属性，在子组件中是只读的 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addEmployee" :model="formData" :rules="rules" label-width="100px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="formData.username" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="formData.mobile" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号" prop="workNumber">
            <el-input v-model="formData.workNumber" placeholder="请输入工号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input v-model="formData.departmentName" placeholder="请选择部门" @focus="getDepartments" />
            <el-tree
              v-if="showTree"
              v-loading="loading"
              :data="treeData"
              :props="{label:'name'}"
              :default-expand-all="true"
              @node-click="selectNode"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker v-model="formData.timeOfEntry" placeholder="请选择入职时间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="转正时间">
            <el-date-picker v-model="formData.correctionTime" placeholder="请选择转正时间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式" prop="formOfEmployment">
            <el-select v-model="formData.formOfEmployment" placeholder="请选择">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils' // 属性转化
import EmployeeEnum from '@/api/constant/employees' // 引入员工的枚举对象
import { addEmployeeData } from '@/api/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: false,
      // EmployeeEnum:EmployeeEnum // 把引入的数据变成data里的数据
      EmployeeEnum // 在data中定义数据
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // depts是数组但不是树形,需要转化成树形结构才可以被el-tree显示
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    // 点击确定时 校验整个表单
    async btnOK() {
      try {
        await this.$refs.addEmployee.validate()
        // 调用新增接口
        await addEmployeeData(this.formData) // 新增员工
        // 告诉父组件更新数据
        // this.$parent 可以直接调用到父组件的实例 实际上就是父组件this
        // console.log(this.$parent)
        this.$parent.getEmployeeList()
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 重置原来的数据
      this.formData = {
        // username: '',
        // mobile: '',
        // formOfEmployment: '',
        // workNumber: '',
        // departmentName: '',
        // timeOfEntry: '',
        // correctionTime: ''
      }
      this.$refs.addEmployee.resetFields() // 重置校验结果
      // this.$parent.showDialog = false
      // update:props名称 --- 可以在父组件直接用sync修饰符处理
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
